package test;

import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author lianmingzhang
 * @create 2017-10-02 下午1:57
 */
public class OReillyByName {
    @Test
    public void getAddress() {
        try {
            InetAddress address = InetAddress.getByName("www.baidu.com");
            System.out.println(address);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获得的主机名
     * 如果查询的地址没有主机名，getHostName()就会返回你提供的四分段地址
     */
    @Test
    public void getHostName() {
        try {
            //InetAddress address = InetAddress.getByName("127.0.0.1");
            InetAddress address = InetAddress.getByName("119.75.216.20");
            System.out.println(address.getHostName());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void getAllByName() {
        try {
            //InetAddress address = InetAddress.getByName("127.0.0.1");
            InetAddress[] address = InetAddress.getAllByName("www.google.com");
            for (InetAddress inetAddress : address) {
                System.out.println(inetAddress);
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }

    /**
     * 内网地址
     * @throws UnknownHostException
     */
    @Test
    public void getMyAddress() throws UnknownHostException {
        System.out.println(InetAddress.getLocalHost());
    }

    @Test
    public void getHostNameByIp() throws IOException {
        InetAddress byName = InetAddress.getByName("www.baidu.com");
        System.out.println(byName.isReachable(4000));
    }


}
